package com.hn.kiss.admin.compoent;

import com.hn.kiss.modules.sys.entity.DictDetail;
import com.hn.kiss.modules.sys.service.IDictDetailService;
import org.springframework.context.ApplicationContext;
import org.thymeleaf.context.ITemplateContext;
import org.thymeleaf.model.IModel;
import org.thymeleaf.model.IModelFactory;
import org.thymeleaf.model.IProcessableElementTag;
import org.thymeleaf.processor.element.AbstractElementTagProcessor;
import org.thymeleaf.processor.element.IElementTagStructureHandler;
import org.thymeleaf.spring5.context.SpringContextUtils;
import org.thymeleaf.templatemode.TemplateMode;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 自定义字典标签
 * <p>Title: SysDictTagProcessor</p>
 * <p>Description: </p>
 *
 * @author dnkj011
 *
 */
public class SysDictTagProcessor extends AbstractElementTagProcessor {

    // 标签名
    private static final String TAG_NAME = "dict";

    // 优先级
    private static final int PRECEDENCE = 10000;

    public SysDictTagProcessor(String dialectPrefix) {
        super(
                // 此处理器将仅应用于HTML模式
                TemplateMode.HTML,

                // 要应用于名称的匹配前缀
                dialectPrefix,

                // 标签名称：匹配此名称的特定标签
                TAG_NAME,

                // 将标签前缀应用于标签名称
                true,

                // 无属性名称：将通过标签名称匹配
                null,

                // 没有要应用于属性名称的前缀
                false,

                // 优先(内部方言自己的优先)
                PRECEDENCE
        );
    }

    /**
     * 处理自定义标签 DOM 结构
     *
     * @param iTemplateContext            模板页上下文
     * @param iProcessableElementTag      待处理标签
     * @param iElementTagStructureHandler 元素标签结构处理器
     */
    @Override
    protected void doProcess(ITemplateContext iTemplateContext, IProcessableElementTag iProcessableElementTag, IElementTagStructureHandler iElementTagStructureHandler) {
    	// 获取 Spring上下文
        ApplicationContext applicationContext = SpringContextUtils.getApplicationContext(iTemplateContext);
        //获取字典service的bean
        //IDictionaryService1 dictService = applicationContext.getBean(IDictionaryService1.class);
        IDictDetailService dictService = applicationContext.getBean(IDictDetailService.class);
        //获取标签的属性值
        String dictType = iProcessableElementTag.getAttributeValue("type");
        String dictName = iProcessableElementTag.getAttributeValue("name");
        String dictClass = iProcessableElementTag.getAttributeValue("class");
        String value = iProcessableElementTag.getAttributeValue("value");
        
        //设置参数
        //Map<String,Object> paraMap = new HashMap<String,Object>();
        /*注释的为之前的字典表
         * paraMap.put("parent", dictType);
        paraMap.put("enable", 1);
        // 根据类型查询出字典列表
        List<Dictionary1> dictList = dictService.getList(paraMap);
        
        // 创建将替换自定义标签的 DOM 结构
        IModelFactory modelFactory = iTemplateContext.getModelFactory();
        IModel model = modelFactory.createModel();

        // 这里是将字典的内容拼装成一个下拉框
        model.add(modelFactory.createOpenElementTag(String.format("select name='%s' class='%s'", dictName, dictClass)));
        
        //创建一个空的提示选项  begin
        model.add(modelFactory.createOpenElementTag(String.format("option value='%s'", "")));
        model.add(modelFactory.createText("请选择"));
        model.add(modelFactory.createCloseElementTag("option"));
        //end
        
        for (Dictionary1 dict : dictList) {
        	if(dict.getId().equals(value)) {
        		model.add(modelFactory.createOpenElementTag(String.format("option selected='selected' value='%s'", dict.getId())));
        	}else {
        		model.add(modelFactory.createOpenElementTag(String.format("option value='%s'", dict.getId())));
        	}
            model.add(modelFactory.createText(dict.getName()));
            model.add(modelFactory.createCloseElementTag("option"));
        }
        model.add(modelFactory.createCloseElementTag("select"));*/
       
        //设置参数
        Map<String,Object> paraMap = new HashMap<String,Object>();
        paraMap.put("dict_type", dictType);
        paraMap.put("is_enable", 1);
        // 根据类型查询出字典列表
        List<DictDetail> dictList = dictService.listByMap(paraMap);
        
        // 创建将替换自定义标签的 DOM 结构
        IModelFactory modelFactory = iTemplateContext.getModelFactory();
        IModel model = modelFactory.createModel();

        // 这里是将字典的内容拼装成一个下拉框
        model.add(modelFactory.createOpenElementTag(String.format("select name='%s' class='%s'", dictName, dictClass)));
        
        //创建一个空的提示选项  begin
        model.add(modelFactory.createOpenElementTag(String.format("option value='%s'", "")));
        model.add(modelFactory.createText("请选择"));
        model.add(modelFactory.createCloseElementTag("option"));
        //end
        for (DictDetail dict : dictList) {
        	if(dict.getDictCode().equals(value)) {
        		model.add(modelFactory.createOpenElementTag(String.format("option value='%s' selected='selected'", dict.getDictCode())));
        	}else {
        		model.add(modelFactory.createOpenElementTag(String.format("option value='%s'", dict.getDictCode())));
        	}
            
            model.add(modelFactory.createText(dict.getDictName()));
            model.add(modelFactory.createCloseElementTag("option"));
        }
        model.add(modelFactory.createCloseElementTag("select"));
        
        
        // 利用引擎替换整合标签
        iElementTagStructureHandler.replaceWith(model, false);
    }
}